<html xmlns:xhtml="http://www.w3.org/1999/xhtml" xmlns:xforms="http://www.w3.org/2002/xforms"
	xmlns:ev="http://www.w3.org/2001/xml-events" xmlns:xs="http://www.w3.org/2001/XMLSchema"
	xmlns="http://www.w3.org/1999/xhtml" xmlns:xxforms="http://orbeon.org/oxf/xml/xforms"
	xmlns:p="http://www.orbeon.com/oxf/pipeline" xmlns:xi="http://www.w3.org/2001/XInclude"
	xmlns:fr="http://orbeon.org/oxf/xml/form-runner">
	<head>
		<title>Numishare: Edit Page</title>
		<link type="text/css" href="http://localhost:8080/cocoon/numishare/xforms.css"
			rel="stylesheet"/>
		<xforms:model>
			<xforms:instance id="page">
				<pages xmlns="">
					<page stub="" id="">
						<title/>
						<short-title/>
						<public>0</public>
						<text/>
					</page>
				</pages>
			</xforms:instance>

			<xforms:instance id="site-info">
				<site_info xmlns="">
					<pages>
						<page stub="" id="">
							<title/>
							<short-title/>
							<public/>
							<text/>
						</page>
					</pages>
				</site_info>
			</xforms:instance>

			<xforms:instance id="status">
				<status/>
			</xforms:instance>

			<xforms:instance id="identifier">
				<id xmlns=""/>
			</xforms:instance>

			<!-- for getting parameters for loading pages -->
			<!--<xforms:action ev:event="xforms-model-construct-done">
				<xforms:setvalue ref="instance('identifier')"
					value="xxforms:get-request-parameter('stub')"/>
				<xforms:setvalue ref="instance('mode')"
					value="xxforms:get-request-parameter('mode')"/>
				<!-\- load XML data on form load -\->
				<!-\-<xforms:send submission="load-submission"/>-\->
			</xforms:action>-->

			<xforms:action ev:event="xforms-ready">
				<xxforms:variable name="mode" select="xxforms:get-request-parameter('mode')"/>
				<xforms:action if="$mode = 'edit'">
					<xforms:setvalue ref="instance('identifier')"
						value="xxforms:get-request-parameter('id')"/>
					<xforms:toggle case="edit-page" ev:event="xforms-model-construct-done"/>
				</xforms:action>
			</xforms:action>

			<xforms:send ev:event="xforms-model-construct-done" submission="load-submission"/>



			<!-- Type for language -->
			<xforms:bind nodeset="instance('page')">
				<xforms:bind nodeset="page">
					<xforms:bind nodeset="@stub" required="true()"/>
					<xforms:bind nodeset="short-title" required="true">
						<xforms:bind constraint="string-length(.) &lt; 24"/>
					</xforms:bind>
					<xforms:bind nodeset="title" required="true()"/>
				</xforms:bind>
			</xforms:bind>
			<xforms:bind nodeset="instance('site-info')">
				<xforms:bind nodeset="pages">
					<xforms:bind nodeset="page">
						<xforms:bind nodeset="@stub" required="true()"/>
						<xforms:bind nodeset="short-title" required="true">
							<xforms:bind constraint="string-length(.) &lt; 24"/>
						</xforms:bind>
						<xforms:bind nodeset="title" required="true()"/>
					</xforms:bind>
				</xforms:bind>
			</xforms:bind>

			<!-- Load File -->
			<xforms:submission id="load-submission" serialization="none" method="get"
				action="/exist/rest/db/numishare/site_info.xml" xxforms:username="admin"
				xxforms:password="admin" replace="instance" instance="site-info"/>

			<!-- Save File -->
			<xforms:submission id="save-submission" ref="instance('site-info')"
				xxforms:username="admin" xxforms:password="admin"
				action="/exist/rest/db/numishare/site_info.xml" method="put" replace="none">
				<xforms:setvalue ev:event="xforms-submit-done" ref="instance('status')">Page
					saved.</xforms:setvalue>
				<xforms:message ev:event="xforms-submit-error" level="modal">Save
					Error!</xforms:message>
			</xforms:submission>

			<xforms:action ev:event="xforms-value-changed" ev:observer="page-title">
				<xforms:setvalue ref="instance('page')/page/@stub"
					value="replace(instance('page')/page/title, ' ', '-')"/>
				<xforms:setvalue ref="instance('page')/page/@stub"
					value="encode-for-uri(lower-case(instance('page')/page/@stub))"/>
			</xforms:action>
			<xforms:action ev:event="xforms-value-changed" ev:observer="edit-page-title">
				<xforms:setvalue
					ref="instance('site-info')//page[@id=instance('identifier')]/@stub"
					value="replace(instance('site-info')//page[@id=instance('identifier')]/title, ' ', '-')"/>
				<xforms:setvalue
					ref="instance('site-info')//page[@id=instance('identifier')]/@stub"
					value="encode-for-uri(lower-case(instance('site-info')//page[@id=instance('identifier')]/@stub))"
				/>
			</xforms:action>
		</xforms:model>

	</head>
	<body>
		<div class="form">
			<xforms:group ref="instance('status')/text()">
				<div class="success">
					<xforms:output ref="instance('status')"/>
				</div>
			</xforms:group>
			<xforms:switch>
				<xforms:case id="create-page">
					<xforms:group ref="instance('page')/page">
						<div class="submission">
							<xforms:trigger appearance="minimal">
								<xforms:label class="file_control"><img
										src="/apps/numishare/images/save.gif" alt="Save"/>
									Save</xforms:label>
								<xforms:action ev:event="DOMActivate">
									<xforms:setvalue ref="@id"
										value="string(digest( string( random( true ) ), 'MD5', 'hex' ))"/>
									<xforms:insert context="instance('site-info')/pages"
										nodeset="./child::node()[last()]"
										origin="instance('page')/page"/>
									<xforms:send submission="save-submission"/>
								</xforms:action>
							</xforms:trigger>
						</div>
						<div class="section">
							<div>
								<xforms:input ref="title" id="page-title">
									<xforms:label>Title</xforms:label>
									<xforms:alert>Required.</xforms:alert>
								</xforms:input>
							</div>
							<div>
								<xforms:input ref="short-title">
									<xforms:label>Short Title</xforms:label>
									<xforms:alert>Required.  24 char max.</xforms:alert>
									<xforms:hint>Text that appears for header link.</xforms:hint>
								</xforms:input>
							</div>
							<div>
								<xforms:input ref="@stub">
									<xforms:label>Stub</xforms:label>
									<xforms:alert>Required.</xforms:alert>
								</xforms:input>
							</div>
							<div>
								<h3>Page Content</h3>
								<xforms:textarea ref="text" mediatype="text/html"/>
							</div>
						</div>
					</xforms:group>
				</xforms:case>
				<xforms:case id="edit-page">
					<div class="submission">
						<xforms:submit submission="save-submission" appearance="minimal">
							<xforms:label class="file_control"><img
									src="/apps/numishare/images/save.gif" alt="Save"/>
								Save</xforms:label>
						</xforms:submit>
					</div>
					<xforms:group ref="instance('site-info')//page[@id=instance('identifier')]">
						<div class="section">
							<div>
								<xforms:input ref="title" id="edit-page-title">
									<xforms:label>Title</xforms:label>
									<xforms:alert>Required.</xforms:alert>
								</xforms:input>
							</div>
							<div>
								<xforms:input ref="short-title">
									<xforms:label>Short Title</xforms:label>
									<xforms:alert>Required.  24 char max.</xforms:alert>
									<xforms:hint>Text that appears for header link.</xforms:hint>
								</xforms:input>
							</div>
							<div>
								<xforms:input ref="@stub">
									<xforms:label>Stub</xforms:label>
									<xforms:alert>Required.</xforms:alert>
								</xforms:input>
							</div>
							<div>
								<h3>Page Content</h3>
								<xforms:textarea ref="text" mediatype="text/html"/>
							</div>
						</div>
					</xforms:group>
				</xforms:case>
			</xforms:switch>

		</div>
		<!--<widget:xforms-instance-inspector id="orbeon-xforms-inspector"
			xmlns:widget="http://orbeon.org/oxf/xml/widget"/>-->
	</body>
</html>
